package org.jeecg.modules.param.charge.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.charge.entity.BikeMemberUserCharge;
import org.jeecg.modules.param.charge.mapper.BikeMemberUserChargeMapper;
import org.jeecg.modules.param.charge.service.IBikeMemberUserChargeService;
import org.jeecg.modules.param.member.entity.BikeMemberUser;
import org.jeecg.modules.param.member.mapper.BikeMemberUserMapper;
import org.jeecg.modules.utils.AliPayKit;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecg.modules.utils.page.PageInfo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @Description: 充值记录
 * @Author: 月月鸟
 * @Date:   2021-05-27
 * @Version: V1.0
 */
@Service
public class BikeMemberUserChargeServiceImpl extends ServiceImpl<BikeMemberUserChargeMapper, BikeMemberUserCharge> implements IBikeMemberUserChargeService {
    @Resource
    private BikeMemberUserChargeMapper bikeMemberUserChargeMapper;
    @Resource
    private BikeMemberUserMapper bikeMemberUserMapper;
    @Override
    public void saveCharge(BikeMemberUser bikeMemberUser) {
        BikeMemberUser memberUser = bikeMemberUserMapper.selectById(bikeMemberUser);
        BikeMemberUserCharge charge = new BikeMemberUserCharge();
        //会员ID
        charge.setUserId(memberUser.getId());
        //充值金额
        charge.setChargeAmount(bikeMemberUser.getPay());
        //更新后的余额
        charge.setBalanceEnd(memberUser.getBalance());
        charge.setChargeCanal("1");
        //后台充值
        charge.setChargeType("1");

        charge.setIsFullRefund(BikeConstant.y_n_no);
        bikeMemberUserChargeMapper.insert(charge);
    }

    @Override
    public Result<?> pageList(Integer pageNo, Integer pageSize, BikeMemberUserCharge bikeMemberUserCharge) {
        Page<BikeMemberUserCharge> page = new Page<>(pageNo, pageSize);
        Page<BikeMemberUserCharge> bikeMemberUserCharges = bikeMemberUserChargeMapper.pageList(page,bikeMemberUserCharge);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setTotal(bikeMemberUserCharges.getTotal());
        pageInfo.setRecords(bikeMemberUserCharges.getRecords());
        pageInfo.setCurrent(bikeMemberUserCharges.getCurrent());
        pageInfo.setSize(bikeMemberUserCharges.getSize());
        return Result.OK(pageInfo);
    }

    @Override
    public Result<?> refundById(String id) {
        BikeMemberUserCharge chargeData = bikeMemberUserChargeMapper.selectById(id);
        Map<String, String> refundMap = null;
        refundMap.put("outTradeNo",chargeData.getOutTradeNo());
        AliPayKit.refund(refundMap);
        return null;
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request, BikeMemberUserCharge bikeMemberUserCharge, Class<BikeMemberUserCharge> bikeMemberUserChargeClass, String title) {
        List<BikeMemberUserCharge> bikeMemberUserCharges = bikeMemberUserChargeMapper.list(bikeMemberUserCharge);
        return ExcelUtils.exportXls(request,bikeMemberUserCharges,BikeMemberUserCharge.class,title);
    }
}
